package com.amumu.netctoss.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sf;
	private static ThreadLocal<Session> thSession = new ThreadLocal<Session>();
	static{
		Configuration conf = new Configuration().configure();
		sf = conf.buildSessionFactory();
	}
	
	public static Session getSession(){
		return sf.openSession();
	}
	
	public static Session getCurrentSession(){
		Session session = thSession.get();
		if(session == null){
			session = sf.openSession();
			thSession.set(session);
		}
		return session;
	}
	
	public static void closeSession(){
		Session session = thSession.get();
		if(session!=null){
			session.close();
			thSession.set(null);
		}
	}
}
